package com.hjq.demo.ui.act

import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.view.View
import android.view.WindowManager
import android.widget.AdapterView
import android.widget.AdapterView.OnItemClickListener
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.textview.MaterialTextView
import com.hjq.base.BaseAdapter
import com.hjq.demo.BuildConfig
import com.hjq.demo.R
import com.hjq.demo.app.AppActivity
import com.hjq.demo.manager.MmkvUtil
import com.hjq.demo.ui.adapter.AppListAdapter
import com.hjq.toast.ToastUtils
import com.hjq.widget.layout.WrapRecyclerView
import timber.log.Timber

/**
 *  应用列表
 */
class AppListAct : AppActivity() {
    private val recy_list: WrapRecyclerView? by lazy { findViewById(R.id.recy_list) }
    private val adapter = AppListAdapter(this);

    private val tv_hospital1: MaterialTextView? by lazy { findViewById(R.id.tv_hospital1) }
    override fun getLayoutId(): Int {
        return R.layout.act_applist
    }

    private fun launchOtherApp(packageName: String) {
        val packageManager = packageManager
        val intent = packageManager.getLaunchIntentForPackage(packageName)
        if (intent != null) {
            startActivity(intent)
        } else {
            Toast.makeText(this, "目标App未安装", Toast.LENGTH_SHORT).show()
        }
    }

    override fun initView() {
        tv_hospital1?.text = "${MmkvUtil.getString(MmkvUtil.DeviceCode, "")} ${
            MmkvUtil.getString(
                MmkvUtil.HosName, "-"
            )
        }";
        val decorView = window.decorView
        val uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        decorView.systemUiVisibility = uiOptions
        getWindow().setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN
        );

        adapter.setOnItemClickListener(object : OnItemClickListener,
            BaseAdapter.OnItemClickListener {
            override fun onItemClick(
                parent: AdapterView<*>?, view: View?, position: Int, id: Long
            ) {
                Timber.d("111")
            }

            override fun onItemClick(recyclerView: RecyclerView?, itemView: View?, position: Int) {
                val path = adapter.getItem(position)?.applicationInfo?.packageName;
                launchOtherApp(path!!)
            }

        })
        recy_list?.adapter = adapter;
        //        if(XXPermissions.isGranted(this,Permission.Query))
        getList();
        ToastUtils.show("下次可点击屏幕进入本列表")
    }

    override fun onBackPressed() {
//        super.onBackPressed()
//        getList();
    }

    private fun getList() {
        val list = packageManager.getInstalledPackages(PackageManager.MATCH_APEX)
        val myList = mutableListOf<PackageInfo?>()
        val preFix = "com.android";
        val preFix1 = "com.mediatek";
        val preFix2 = "com.google";
        val preFix3 = "com.wapi";
        val preFix4 = "android";
        val preFix5 = "com.incar";
//        过滤掉系统应用
        for (app in list) {
            val appName = getContext().packageManager.getApplicationLabel(app.applicationInfo)
            val pkg = app.packageName;

            val isAndroid = pkg.startsWith(preFix);
            val isMediatek = pkg.startsWith(preFix1);
            val isGoogle = pkg.startsWith(preFix2);
            val is3 = pkg.startsWith(preFix3);
            val is4 = pkg.startsWith(preFix4);
            val is5 = pkg.startsWith(preFix5);
            val isMe = pkg.equals(BuildConfig.APPLICATION_ID);
            val preInstall =
                app.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM !== 0;// preInstall = true 是预装
            if (!preInstall && !isAndroid && !isGoogle && !isMediatek && !is3 && !is4 && !is5 && !isMe) {
                myList.add(app)
            } else {
                Timber.w("过滤了: ${appName} $pkg，$preInstall $isAndroid $isGoogle $isMediatek $is3 $is4 $is5")
            }
        }


//        Timber.d("" + list.size)
        adapter.setData(myList)
    }

    override fun initData() {

    }
}